#include "lxmusic.h"
#include "ui_lxmusic.h"
#include<QFile>



LXMusic::LXMusic(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LXMusic)
    ,_currentPlayPage(nullptr)
{
    ui->setupUi(this);

    //初始化界面
    initUi();

    //初始化播放器
    initPlayer();

    //绑定信号和槽函数
    connectSignalAndSlots();

    //初始化数据库
    initSQlite();

    //初始化musiclist并同步数据到页面中
    initMusiclist();

}

LXMusic::~LXMusic()
{
    delete ui;
}

void LXMusic::initUi(){
    //给窗口设置标题栏无边框
    this->setWindowFlag(Qt::FramelessWindowHint);

    //设置窗口背景透明
    this->setAttribute(Qt::WA_TranslucentBackground);

    //更换主窗口图标
    this->setWindowIcon(QIcon(":/images/logo.png"));
    this->setWindowTitle("LXMusic");

    //禁用最大化图标
    ui->maxButton->setEnabled(false);

    //初始化系统托盘
    this->createTray();

    //设置窗口阴影部分
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0,0);//设置阴影偏移量
    shadowEffect->setColor(Qt::black);//设置阴影颜色
    shadowEffect->setBlurRadius(10);//设置阴影半径
    this->setGraphicsEffect(shadowEffect);

    //设置btform内容
    ui->recommend->setIconAndTextAndId(":/images/recommend.png","推荐",0);
    ui->musicHall->setIconAndTextAndId(":/images/musicHall.png", "乐馆", 1);
    ui->audio->setIconAndTextAndId(":/images/audio.png", "视频", 2);
    ui->likeMusic->setIconAndTextAndId(":/images/likeMusic.png", "喜欢", 3);
    ui->recentMusic->setIconAndTextAndId(":/images/recentMusic.png", "最近播放", 4);
    ui->localMusic->setIconAndTextAndId(":/images/localMusic.png", "本地和下载", 5);

    //将图片打乱
    srand(time(0));
    ui->recMusicBox->initRecBoxUi(randomPticture(),1);
    ui->supplyMusicBox->initRecBoxUi(randomPticture(),2);

    //初始化commandpage界面
    ui->likeMusicPage->setCommonPageUi("我喜欢的音乐",":/images/likemusic.jpg");
    ui->likeMusicPage->setMusicListType(PageType::LIKEPAGE);
    ui->recentMusicPage->setCommonPageUi("最近播放",":/images/recentmusic.jpg");
    ui->recentMusicPage->setMusicListType(PageType::RECENTPAGE);
    ui->localMusicPage->setCommonPageUi("本地下载",":/images/localmusic.jpg");
    ui->localMusicPage->setMusicListType(PageType::LOCALPAGE);

    //默认在推荐页面
    ui->stackedWidget->setCurrentIndex(0);
    //默认播放页面在本地下载页面
    ui->localMusic->showAnimation();

    // 初始化_currentPage
    _currentPlayPage = ui->localMusicPage;

    //创建volumetool窗口
    _volumetool = new VolumeTool(this);

    //设播放图标
    ui->playButton->setIcon(QIcon(":/images/play1.png"));
    ui->playModeButton->setIcon(QIcon(":/images/loopPlay.png"));
    ui->playModeButton->setToolTip("列表循环");

    //初始化lrcPage页面
    _lrcWord = new LrcPage(this);
    _lrcWord->hide();

    // lrcPage添加动画效果
    _animal = new QPropertyAnimation(_lrcWord, "geometry", this);
    _animal->setDuration(250);
    _animal->setStartValue(QRect(10, 10+_lrcWord->height(),_lrcWord->width(), _lrcWord->height()));
    _animal->setEndValue(QRect(10, 10, _lrcWord->width(), _lrcWord->height()));
}



void LXMusic::initPlayer()
{
    //创建播放器
    _player = new QMediaPlayer(this);
    //创建播放列表
    _playList = new QMediaPlaylist(this);

    //设置播放模式:默认为循环播放
    _playList->setPlaybackMode(PlaybackMode::Loop);

    //默认音量设置为20
    QAudioOutput *audioOutput = new QAudioOutput(this);
    _player->setAudioOutput(audioOutput);
    _player->setAudioOutput(audioOutput);
    audioOutput->setVolume(0.5);// 设置音量（范围是 0.0 到 1.0）20%

}

//绑定BtForm相关控件信号与槽函数
void LXMusic::connectSignalAndSlots()
{
    //绑定BtForm信号
    connect(ui->recommend, &BtForm::btClick, this, &LXMusic::onBtformClicked);
    connect(ui->musicHall, &BtForm::btClick, this, &LXMusic::onBtformClicked);
    connect(ui->audio, &BtForm::btClick, this, &LXMusic::onBtformClicked);
    connect(ui->likeMusic, &BtForm::btClick, this, &LXMusic::onBtformClicked);
    connect(ui->recentMusic, &BtForm::btClick, this, &LXMusic::onBtformClicked);
    connect(ui->localMusic, &BtForm::btClick, this, &LXMusic::onBtformClicked);

    //绑定界面更新信号
    connect(ui->likeMusicPage,&CommonPage::updateLikeMusic, this,&LXMusic::onUpdateLikeMusic);
    connect(ui->localMusicPage,&CommonPage::updateLikeMusic, this,&LXMusic::onUpdateLikeMusic);
    connect(ui->recentMusicPage,&CommonPage::updateLikeMusic, this,&LXMusic::onUpdateLikeMusic);

    //绑定播放相关按钮图标
    connect(ui->playButton,&QPushButton::clicked, this, &LXMusic::onPlayClicked);
    connect(_player,&QMediaPlayer::playbackStateChanged,this, &LXMusic::onPlayStausTochangeIcon);
    connect(ui->playUpButton,&QPushButton::clicked, this, &LXMusic::onPlayUpClicked);
    connect(ui->playDownButton,&QPushButton::clicked, this, &LXMusic::onPlayDownClicked);

    // 监听播放结束信号，自动播放下一个
    connect(_player, &QMediaPlayer::playbackStateChanged, this, &LXMusic::finishedAutoplay);
    //播放模式更换，更改图标
    connect(ui->playModeButton,&QPushButton::clicked, this, &LXMusic::onPlaybackModeClicked);
    connect(_playList,&QMediaPlaylist::playbackModeChanged, this, &LXMusic::onPlaybackModeChange);

    //关联CommonPage发出的播放所有信号，实现播放对应页面的歌曲
    connect(ui->likeMusicPage,&CommonPage::playAll, this, &LXMusic::onPlayAll);
    connect(ui->localMusicPage,&CommonPage::playAll, this, &LXMusic::onPlayAll);
    connect(ui->recentMusicPage,&CommonPage::playAll, this, &LXMusic::onPlayAll);

    //关联CommonPage发出的双击音乐播放信号，实现播放对应页面的歌曲
    connect(ui->likeMusicPage,&CommonPage::playMusicbyIndex, this, &LXMusic::playMusicByIndex);
    connect(ui->localMusicPage,&CommonPage::playMusicbyIndex, this, &LXMusic::playMusicByIndex);
    connect(ui->recentMusicPage,&CommonPage::playMusicbyIndex, this, &LXMusic::playMusicByIndex);

    // 播放列表中的索引发⽣改变，此时将播放⾳乐收藏到历史记录中
    connect(_playList, &QMediaPlaylist::currentIndexChanged, this,&LXMusic::onCurrentIndexChanged);

    //接受volumetool发来的静音信号
    connect(_volumetool, &VolumeTool::setSilence, this, &LXMusic::setMusicSilence);

    //接受volumetool发来的信号，设置音量大小
    connect(_volumetool,&VolumeTool::setMusicVolume, this, &LXMusic::setMusicVolume);

    //监听_player总时间的变化
    connect(_player, &QMediaPlayer::durationChanged, this, &LXMusic::onDurationChanged);
    //监听_player当前播放位置变化
    connect(_player,&QMediaPlayer::positionChanged, this, &LXMusic::onPositionChanged);

    //监听进度条的变化，改变当前播放时间
    connect(ui->processBar, &MusicSlider::setMusicSliderPostion, this, &LXMusic::onMusicSliderChanged);

    //监听歌曲的改变，显示歌名歌手和图片和加载歌词
    connect(_player,&QMediaPlayer::metaDataChanged, this, &LXMusic::onMetaDataChanged);

    //点击歌词按钮时，弹出歌词窗口
    connect(ui->lyricButton, &QPushButton::clicked, this, &LXMusic::onLrcWordClicked);

    //当播放状态发生改变的时候，更新BtForm上的动画
    connect(_player,&QMediaPlayer::playbackStateChanged, this, &LXMusic::updateBtFormAnimation);

}

//将图片路径打乱
QJsonArray LXMusic::randomPticture()
{
    //添加推荐图片名称
    QVector<QString> imageName;
    for(int i = 1; i <= 24; ++i){
        if(i < 10)
            imageName << "00" + QString::number(i) + ".jpg";
        else
            imageName << "0" + QString::number(i) + ".jpg";
    }

    //打乱顺序
    std::random_shuffle(imageName.begin(), imageName.end());

    QJsonArray objArray;
    int n = imageName.size();
    for(int i = 0; i < n; ++i){
        QJsonObject obj;
        obj.insert("imagePath", ":/images/recommend/" + imageName[i]);

        // arg(i, 3, 10, QCchar('0'))
        // i：要放⼊%1位置的数据
        // 3: 三位数
        // 10：表⽰⼗进制数
        // QChar('0')：数字不够三位，前⾯⽤字符'0'填充
        QString strText = QString("推荐-%1").arg(i + 1, 3, 10, QChar('0'));
        obj.insert("text", strText);
        objArray.append(obj);
    }

    return objArray;
}

//更改播放列表中的歌曲
void LXMusic::playAllOfPage(CommonPage *page, int index)
{
    _currentPlayPage = page;
   // QUrl url = _playList->currentMedia();

    //清空播放列表中的歌
    _playList->clear();

    //将对应页面中存在的歌，加入到播放列表中
    page->addMusicToPlayList(_musicList,_playList);

    //设置当前播放列表的索引
    _playList->setCurrentIndex(index);

    _isManualPlay = true;
    //播放音乐
    _player->setSource(_playList->currentMedia());
    _player->play();

    _isManualPlay = false;
}

void LXMusic::setMusicHistoryByUrl(QUrl url)
{
    for(auto& music : _musicList){
        if(music.getMusicUrl() == url){
            music.setIsHistory(true);
        }
    }
}

void LXMusic::initSQlite()
{
    //加载驱动
    sqlite = QSqlDatabase::addDatabase("QSQLITE");

    //设置数据库名称
    sqlite.setDatabaseName("LXMusic.db");

    //打开数据库
    if(!sqlite.open()){
        QMessageBox::critical(this, "打开QQMusicDB失败",sqlite.lastError().text());
        return;
    }
    //qDebug() << "数据库连接成功";

// 4. 创建数据库表
    QString sql = ("CREATE TABLE IF NOT EXISTS musicInfo(\
            id INTEGER PRIMARY KEY AUTOINCREMENT,\
            musicId varchar(200) UNIQUE,\
            musicName varchar(50),\
            musicSinger varchar(50),\
            albumName varchar(50),\
            duration BIGINT,\
            musicUrl varchar(256),\
            isLike INTEGER,\
            isHistory INTEGER)"
        );
    QSqlQuery query;
    if(!query.exec(sql)){
        QMessageBox::critical(this, "创建数据库表失败",query.lastError().text());
        return;
    }
    //qDebug()<<"创建 [musicInfo] 表成功!!!";
}
//根据数据库初始化musiclist
void LXMusic::initMusiclist()
{
    //初始化musiclist
    _musicList.readFromDB();
    //将数据更新到各自的页面中
    ui->recentMusicPage->refreshPage(_musicList);
    ui->likeMusicPage->refreshPage(_musicList);
    ui->localMusicPage->refreshPage(_musicList);

    //默认将localMusicPage页面中的歌曲设置进playList中
    ui->localMusicPage->addMusicToPlayList(_musicList,_playList);

}
//初始化系统托盘
void LXMusic::createTray()
{
    //增加系统托盘
    QSystemTrayIcon* tryIcon = new QSystemTrayIcon(this);
    tryIcon->setIcon(QIcon(":/images/logo.png"));
    //创建托盘菜单
    QMenu* menu = new QMenu(this);
    menu->addAction("还原",this, &QWidget::showNormal);
    menu->addSeparator();
    menu->addAction("退出", this, &LXMusic::quitLXMusic);

    //将托盘菜单添加到托盘图标
    tryIcon->setContextMenu(menu);

    tryIcon->showMessage("提示", "应用程序已最小化到托盘", QSystemTrayIcon::Information, 3000);

    //为托盘图标添加点击事件处理。当左键点击时还原窗口
    connect(tryIcon, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
        if (reason == QSystemTrayIcon::Trigger) { // 左键点击
            this->showNormal(); // 还原窗口
        }
    });

    //显示系统托盘
    tryIcon->show();
}

void LXMusic::quitLXMusic()
{
    //qDebug() << "退出按钮被点击";
    //关闭前写入数据库中
    _musicList.writeToDB();
    //qDebug() << "写入完成";
    //关闭数据库
    sqlite.close();
    //关闭程序
    QApplication::quit();
}


void LXMusic::on_quitButton_clicked()
{
    // 点击关闭按钮时，程序不退出，隐藏掉
    // ⽤⼾可以从系统托盘位置选择显⽰或者关闭
    hide();
}

void LXMusic::onBtformClicked(int pageId)
{
    //获取btform类型的孩子
    QList<BtForm*> btFormList = this->findChildren<BtForm*>();
    for(auto btForm : btFormList){
        if(btForm->getPageId() != pageId){
            btForm->clearBackground();
        }
        else{
            btForm->setBackground();
        }
    }

    ui->stackedWidget->setCurrentIndex(pageId);

    //设为false防止乱移
    _isDrag = false;

   // qDebug() << pageId;
}

void LXMusic::mousePressEvent(QMouseEvent* event){
    if(event->button() == Qt::LeftButton){
        _isDrag = true;

        _dragPosition = event->globalPosition().toPoint() - geometry().topLeft();
        event->accept();
    }
    QWidget::mousePressEvent(event);

}

void LXMusic::mouseMoveEvent(QMouseEvent* event){
    if(event->buttons() == Qt::LeftButton && _isDrag){
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
    QWidget::mouseMoveEvent(event);
}



void LXMusic::on_volumeButton_clicked()
{
    //获取volumeButton左上角的坐标
    QPoint point = ui->volumeButton->mapToGlobal(QPoint(0,0));

    //计算volume窗⼝的左上⻆位置
    // 让该窗⼝显⽰在⿏标点击的正上⽅
    // ⿏标位置：减去窗⼝宽度的⼀半，以及⾼度恰巧就是窗⼝的左上⻆
    QPoint volumeLeftTop = point - QPoint(_volumetool->width()/2, _volumetool->height());

    // 微调窗⼝位置
    //volumeLeftTop.setY(volumeLeftTop.y());
    volumeLeftTop.setX(volumeLeftTop.x()+15);

    // 3. 移动窗⼝位置
    _volumetool->move(volumeLeftTop);

    _volumetool->show();
}



void LXMusic::on_addLocalButton_clicked()
{
    //创建QFileDialog类对象
    QFileDialog fileDialog(this);
    fileDialog.setWindowTitle("添加本地音乐");

    //设置为打开窗口
    fileDialog.setAcceptMode(QFileDialog::AcceptOpen);

    //设置对话框模式
    //设置该窗口为打开可选择一个或多个文件的窗口
    fileDialog.setFileMode(QFileDialog::ExistingFiles);

    //设置窗口过滤器
    QStringList mimeList;
    mimeList << "application/octet-stream";
    fileDialog.setMimeTypeFilters(mimeList);

    //设置对话框默认的打开路径,设置⽬录为当前⼯程所在⽬录
    QDir dir(QDir::currentPath());
    dir.cdUp();
    dir.cdUp();
    QString path = dir.path() + "/music";
    fileDialog.setDirectory(path);

    if(QFileDialog::Accepted == fileDialog.exec()){
        //获取btform类型的孩子
        QList<BtForm*> btFormList = this->findChildren<BtForm*>();
        for(auto btForm : btFormList){
            if(btForm->getPageId() != 5){
                btForm->clearBackground();
                btForm->hideAnimation();
            }
            else{
                btForm->showAnimation();
                btForm->setBackground();
            }
        }

        //修改stackPage页面
        ui->stackedWidget->setCurrentIndex(5);

        // 获取对话框的返回值
        QList<QUrl> urls = fileDialog.selectedUrls();

        //将音乐保存到musiclist中
        _musicList.addMusicByUrl(urls);

        //将添加的音乐显示在localPage中
        ui->localMusicPage->refreshPage(_musicList);

        //将音乐添加到_playlist中
        ui->localMusicPage->addMusicToPlayList(_musicList,_playList);
        //_player->setSource(_playList->currentMedia());
    }
}

void LXMusic::onUpdateLikeMusic(bool isLike, QString musicId)
{
    //qDebug() << "22";
    // 1. 找到该⾸歌曲，并更新对应Music对象信息
    auto it = _musicList.findMusicById(musicId);
    if(it != _musicList.end())
    {
        it->setIsLike(isLike);
    }

    // 2. 通知三个⻚⾯更新⾃⼰的数据
    ui->likeMusicPage->refreshPage(_musicList);
    ui->localMusicPage->refreshPage(_musicList);
    ui->recentMusicPage->refreshPage(_musicList);
}

//播放暂停音乐，槽函数
void LXMusic::onPlayClicked()
{
    if(_player->source().isEmpty()){
        QUrl url = _playList->currentMedia();
        //qDebug() << url;
        if(!url.isEmpty()){
            setMusicHistoryByUrl(url);
            //刷新最近播放界面
            ui->recentMusicPage->refreshPage(_musicList);
            //设置播放源
            _player->setSource(url);
        }
    }
    //正在播放，则暂停，否则播放
    if(_player->isPlaying()){
        _player->pause();
    }
    else{
        _player->play();
    }
}

void LXMusic::onPlayStausTochangeIcon()
{
    if(_player->playbackState() == QMediaPlayer::PlayingState){
        // 播放状态
        ui->playButton->setIcon(QIcon(":/images/play2.png"));
    }
    else{
        // 暂停状态
        ui->playButton->setIcon(QIcon(":/images/play1.png"));
    }
}

//切换到上一首歌
void LXMusic::onPlayUpClicked()
{
    //qDebug() << " shangyishou";
    _isManualPlay = true;//避免调用finishedAutoplay函数
    _player->setSource(_playList->previous());
    _player->play();
    _isManualPlay = false;
    //qDebug() << " send";
}

//切换到下一首歌
void LXMusic::onPlayDownClicked()
{

    _isManualPlay = true;//避免调用finishedAutoplay函数
    _player->setSource(_playList->next());
    _player->play();
    _isManualPlay = false;
}

//当点击播放模式按钮的时候，切换播放模式
void LXMusic::onPlaybackModeClicked()
{
    if(_playList->playbackMode() == PlaybackMode::Loop){
        _playList->setPlaybackMode(PlaybackMode::Random);
    }
    else if(_playList->playbackMode() == PlaybackMode::Random){
        _playList->setPlaybackMode(PlaybackMode::Sequential);
    }
    else if(_playList->playbackMode() == PlaybackMode::Sequential)
    {
        _playList->setPlaybackMode(PlaybackMode::CurrentItemLoop);
    }else if(_playList->playbackMode() == PlaybackMode::CurrentItemLoop){
        _playList->setPlaybackMode(PlaybackMode::Loop);
    }
    else{
        qDebug() << "功能暂未支持";
    }
}

//监听播放结束信号，自动播放下一个
void LXMusic::finishedAutoplay(QMediaPlayer::PlaybackState state)
{
    if (_isManualPlay) {
        return; // 如果是手动播放，忽略信号处理
    }
    if (state == QMediaPlayer::StoppedState ) {
        QUrl nextMedia = _playList->next();
        if (!nextMedia.isEmpty()) {
            _player->setSource(nextMedia);
            _player->play();
        } else {
            qDebug() << "Playback finished.";
        }
    }
}

//播放模式切换，会自动触发信号，该槽函数与播放模式信号进行绑定
void LXMusic::onPlaybackModeChange(PlaybackMode playbackMode)
{
    if(playbackMode == PlaybackMode::Loop){
        ui->playModeButton->setIcon(QIcon(":/images/loopPlay.png"));
        ui->playModeButton->setToolTip("列表循环");
    }
    else if(playbackMode == PlaybackMode::Random){
        ui->playModeButton->setIcon(QIcon(":/images/randomPlay.png"));
        ui->playModeButton->setToolTip("随机播放");
    }
    else if(playbackMode == PlaybackMode::CurrentItemLoop){
        ui->playModeButton->setIcon(QIcon(":/images/currentItemLoop.png"));
        ui->playModeButton->setToolTip("单曲循环");
    }else if(playbackMode == PlaybackMode::Sequential){
        ui->playModeButton->setIcon(QIcon(":/images/sequentialPlay.png"));
        ui->playModeButton->setToolTip("顺序播放");
    }
    else{
        qDebug() << "功能暂未支持";
    }
}

//实现播放对应页面中的歌曲
void LXMusic::onPlayAll(PageType type)
{
    CommonPage* page = nullptr;
    switch(type){
        case PageType::LIKEPAGE:
        {
            page = ui->likeMusicPage;
            break;
        }
        case PageType::LOCALPAGE:
        {
            page = ui->localMusicPage;
            break;
        }
        case PageType::RECENTPAGE:
        {
            page = ui->recentMusicPage;
            break;
        }
        default:
            qDebug() << "功能暂未支持";
            break;
    }
        playAllOfPage(page,0);
}

//播放对应页面中，index索引下的歌曲
void LXMusic::playMusicByIndex(CommonPage *page, int index)
{
    playAllOfPage(page, index);
}

void LXMusic::onCurrentIndexChanged(int index)
{
    //qDebug() << "111111";
    QString musicId = _currentPlayPage->getMusicIdByIndex(index);
    if(musicId.isEmpty())return;

    //qDebug() << "222222";

    auto it = _musicList.findMusicById(musicId);
    if(it != _musicList.end()){
        // 将该⾳乐设置为历史播放记录
        it->setIsHistory(true);
    }
    //qDebug() << "222222";
    //刷新最近播放界面
    ui->recentMusicPage->refreshPage(_musicList);
}

//设置静音
void LXMusic::setMusicSilence(bool isMuted)
{
    QAudioOutput* audioOutput = _player->audioOutput();
    audioOutput->setMuted(isMuted);
}

//接受volumetool的信号，设置音量
void LXMusic::setMusicVolume(int volumeRatio)
{
    QAudioOutput* audioOutput = _player->audioOutput();
    audioOutput->setVolume(volumeRatio / 100.0);
}
//更新总时间
void LXMusic::onDurationChanged(qint64 duration)
{
    ui->totalTimeLabel->setText((QString("%1:%2").arg(duration/1000/60, 2, 10,QChar('0')).arg(duration/1000%60,2,10,QChar('0'))));
}
//更新当前播放时间并设置进度条的位置
void LXMusic::onPositionChanged(qint64 position)
{
    ui->currentTimeLabel->setText((QString("%1:%2").arg(position/1000/60, 2, 10,QChar('0')).arg(position/1000%60,2,10,QChar('0'))));

    //设置进度条的位置
    ui->processBar->syncSlide((float)position / (float)_player->duration());

    if(_playList->getCurrentIndex() >= 0){
        //显示歌词
        _lrcWord->showLrcWord(position);
    }
}
//根据进度条位置，更新当前播放时间
void LXMusic::onMusicSliderChanged(float value)
{
    qint64 duration = (qint64)(_player->duration() * value);
    ui->currentTimeLabel->setText((QString("%1:%2").arg(duration/1000/60, 2, 10,QChar('0')).arg(duration/1000%60,2,10,QChar('0'))));

    //设置当前播放时间
    _player->setPosition(duration);
}

//音乐改变时，更新图片歌名歌手
void LXMusic::onMetaDataChanged()
{
    QString musicName = _player->metaData().value(QMediaMetaData::Title).toString();
    QString singerName = _player->metaData().value(QMediaMetaData::Author).toString();
    QString musicId = _currentPlayPage->getMusicIdByIndex(_playList->getCurrentIndex());
    //qDebug() << "musicId::" << musicId;

    // if(musicName.isEmpty() || singerName.isEmpty())
    // {
        auto it = _musicList.findMusicById(musicId);
        if(it != _musicList.end())
        {
            musicName = it->getMusicName();
            singerName = it->getSingerName();
            _lrcWord->parseLrc(it->getLrcFilePath());
        }
    //}

    //设置歌手名称、歌名
    ui->musicNameLabel->setText(musicName);
    ui->musicSingerLabel->setText(singerName);
    _lrcWord->setMusicName(musicName);
    _lrcWord->setSingerName(singerName);

    //获取图片
    QVariant coverImage =  _player->metaData().value(QMediaMetaData::ThumbnailImage);
    if(coverImage.isValid()){
        // 获取封⾯图⽚成功
        QImage image = coverImage.value<QImage>();
        // 设置封⾯图⽚
        ui->musicCoverLabel->setPixmap(QPixmap::fromImage(image));
        // 缩放填充到整个Label
        ui->musicCoverLabel->setScaledContents(true);

        _currentPlayPage->setImageLabel(QPixmap::fromImage(image));
    }
    else{
        //qDebug() << " 11111";
        if(_currentPlayPage == ui->localMusicPage){
            ui->musicCoverLabel->setPixmap(QPixmap(":/images/localmusic.jpg"));
            // 缩放填充到整个Label
            ui->musicCoverLabel->setScaledContents(true);
            _currentPlayPage->setImageLabel(QPixmap(":/images/localmusic.jpg"));
        }
        else if(_currentPlayPage == ui->likeMusicPage){
            ui->musicCoverLabel->setPixmap(QPixmap(":/images/likemusic.jpg"));
            // 缩放填充到整个Label
            ui->musicCoverLabel->setScaledContents(true);
            _currentPlayPage->setImageLabel(QPixmap(":/images/likemusic.jpg"));
        }
        else{
            ui->musicCoverLabel->setPixmap(QPixmap(":/images/recentmusic.jpg"));
            // 缩放填充到整个Label
            ui->musicCoverLabel->setScaledContents(true);
            _currentPlayPage->setImageLabel(QPixmap(":/images/recentmusic.jpg"));
        }
    }
}

void LXMusic::onLrcWordClicked()
{
    _lrcWord->show();
    _animal->start();
}


void LXMusic::on_minButton_clicked()
{
    showMinimized();
}


void LXMusic::on_skinButton_clicked()
{
    QMessageBox::information(this, "温馨提⽰", "正在加班紧急⽀持中...");
}

void LXMusic::updateBtFormAnimation()
{
    int index = ui->stackedWidget->indexOf(_currentPlayPage);
    if(-1 == index){
        qDebug() << "该页面不存在";
        return ;
    }

    QList<BtForm*> btlist = findChildren<BtForm*>();
    for(auto& btForm : btlist){
        if(btForm->getPageId() != index){
            btForm->hideAnimation();
        }
        else{
            btForm->showAnimation();
        }
    }
}
